﻿using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using SMSAssistant.Model;

namespace SMSAssistant
{
    public class ItemViewModel : INotifyPropertyChanged
    {
        Message _message;

        public Message Message
        {
            get { return _message; }
            set
            {
                _message = value;
                NotifyPropertyChanged("Message");
            }
        }

        public ItemViewModel()
        {

        }

        public ItemViewModel(Message message)
        {
            _message = message;
        }

        private string _lineOne;
        /// <summary>
        /// Sample ViewModel property; this property is used in the view to display its value using a Binding.
        /// </summary>
        /// <returns></returns>
        public string LineOne
        {
            get
            {
                if (_message.Category == Category.OUT)
                    return Resources.Resource.Me;
                else if (_message.Contact != null)
                    return _message.Contact.ToString();
                else
                    return "Unknow";
            }
            set
            {
                if (value != _lineOne)
                {
                    _lineOne = value;
                    NotifyPropertyChanged("LineOne");
                }
            }
        }

        private string _lineTwo;
        /// <summary>
        /// Sample ViewModel property; this property is used in the view to display its value using a Binding.
        /// </summary>
        /// <returns></returns>
        public string LineTwo
        {
            get
            {
                return _message.Content;
            }
            set
            {
                if (value != _lineTwo)
                {
                    _lineTwo = value;
                    NotifyPropertyChanged("LineTwo");
                }
            }
        }

        private string timeToDisplay;
        /// <summary>
        /// Sample ViewModel property; this property is used in the view to display its value using a Binding.
        /// </summary>
        /// <returns></returns>
        public string TimeToDisplay
        {
            get
            {
                if (_message.LastReviewTime.HasValue)
                {
                    var time = _message.LastReviewTime.Value.ToLocalTime();
                    if (time.Date == DateTime.Now.Date)
                        return time.ToString("HH:mm");
                    else if (time.Month==DateTime.Now.Month)
                        return time.ToString("M/d");
                    else
                        return time.ToString("MMM");
                }
                else
                    return string.Empty;
            }
            set
            {
                timeToDisplay = value;
                NotifyPropertyChanged("TimeToDisplay");
            }
        }


        public event PropertyChangedEventHandler PropertyChanged;
        private void NotifyPropertyChanged(String propertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (null != handler)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }
}